package com.yunhe.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;

import com.yunhe.bean.news;
import com.yunhe.inter.newsInter;
import com.yunhe.util.SqlSessionFactoryUtil;

/**
 * Servlet implementation class insertNews
 */
@WebServlet("/insertNews")
public class insertNews extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public insertNews() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//确认接收编码
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		//获取sqlSession
		SqlSession sqlSession = SqlSessionFactoryUtil.openSqlSession();
		//建立接口映射
		newsInter mapper=sqlSession.getMapper(newsInter.class);
		//创建新闻对象用于接收修改信息
		news ns=new news();
		//ns赋值
		if(request.getSession().getAttribute("user_id")!=null && request.getSession().getAttribute("user_id")!=""){
			ns.setUser_id(Integer.parseInt(request.getParameter("user_id")));
		}
		if(request.getParameter("news_head")!=null && request.getParameter("news_head")!=""){
			ns.setNews_head(request.getParameter("news_head"));
		}
		if(request.getParameter("news_content")!=null && request.getParameter("news_content")!=""){
			ns.setNews_content(request.getParameter("news_content"));
		}
		//执行新增
		mapper.insertNews(ns);
		sqlSession.commit();
		sqlSession.close();
		response.sendRedirect("manage/news.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
